﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Advertisements;

public class LoseUI : MonoBehaviour
{
    public UnityEngine.UI.Text Score;   // current score

    public UnityEngine.UI.Text Best;    // best score

    private int playerScore;            // score tmp

    private int star;                   // star number
#if UNITY_IOS
private string gameId = "1600137";
#elif UNITY_ANDROID
    private string gameId = "1600136";
#endif
    IEnumerator ShowAdWhenReady()
    {
        if (!Advertisement.isInitialized)
            Debug.Log("Advertisement is not isInitialized");
        else
            Debug.Log("Advertisement is  isInitialized");
        while (!Advertisement.IsReady())
        {
            yield return null;
        }
        Debug.Log("Advertisement is  ready");
        Advertisement.Show();
    }
    public void ShowU3DAd()
    {
        Advertisement.Initialize(gameId, false);

        StopCoroutine("ShowAdWhenReady");
        StartCoroutine("ShowAdWhenReady");
    }
    public void ShowAd()
    {
        int ads = DataCenter.Instance.caculatePlayResult++ % DataCenter.Instance.MaxLoop;
        if (ads == 0)
        {
            Debug.Log("RequestRewardBasedVideo ads");
             GoogleMobileAdsScript.Instance.RequestRewardBasedVideo();  //发布国外的时候 打开
            //   ShowU3DAd();//发布国内的时候 打开
        }
        else
        {
            StopCoroutine("ShowAdWhenReady");
            Debug.Log("RequestInterstitial ads");
            GoogleMobileAdsScript.Instance.RequestInterstitial();
        }
    }
    void Start()
    {
        //         GoogleMobileAdsScript.Instance.IsUnityADS = !GoogleMobileAdsScript.Instance.IsUnityADS;
        // 
        // 
        //         if (GoogleMobileAdsScript.Instance.IsUnityADS )
        //         {
        //             UnityAdsHelper.ShowAd();
        //         }else
        //             GoogleMobileAdsScript.Instance.RequestInterstitial();
        if (PlayerPrefs.GetInt("IsBuy") == 0)
            ShowAd();

        if (PLayerInfo.MODE != 1)
            playerScore = PLayerInfo.Info.Score + (PLayerInfo.MapPlayer.Level - 1) * 5000;
        else
            playerScore = PLayerInfo.Info.Score;
        // display score text
        Score.text = playerScore.ToString();
        // display best score text
        Best.text = getBestScore(playerScore).ToString();
        Dictionary<string, object> dic = new Dictionary<string, object>();
        dic.Add("FailLevel", PLayerInfo.MapPlayer.Level);
        Third.Analytics.Instance.SendEvent("Login", dic);

        EventCenter.AddListener(EGameEvent.eGoogleAdsFailed, ShowUnityADS);
    }
    private void ShowUnityADS()
    {
     //   UnityAdsHelper.ShowAd();
    }
    /// <summary>
    /// compare score with best score
    /// </summary>
    /// <param name="score">score by player</param>
    /// <returns>best score</returns>
    int getBestScore(int score)
    {
        if (PLayerInfo.MODE != 1)
        {
            if (score > PLayerInfo.MapPlayer.HightScore)
            {
                PLayerInfo.MapPlayer.HightScore = score;
                PlayerPrefs.SetInt(PLayerInfo.KEY_CLASSIC_HISCORE, PLayerInfo.MapPlayer.HightScore);
            }
        }
        return PLayerInfo.MapPlayer.HightScore;
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener(EGameEvent.eGoogleAdsFailed, ShowUnityADS);
    }

}
